package map.DSU;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class findWay2 {
    private static int[] father;
    public static void init(int n){
        father = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            father[i] = i;
        }
    }
    public static int find(int u) {
        if (u == father[u]){
            return u;
        } else {
            return find(father[u]);
        }
    }
    public static void join(int u, int v) {
        u = find(u);
        v = find(v);
        if (u != v){
            father[v] = u;
        }
    }
    public static boolean isSame(int u, int v){
        return find(u) == find(v);
    }
    static class Edge{
        int s;
        int t;
        public Edge(int s, int t){
            this.s = s;
            this.t = t;
        }
    }
    static class Node {
        int id;
        int in;
        int out;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Edge> edges = new ArrayList<>();
        Node[] nodeMap = new Node[n + 1];
        for (int i = 0; i < n + 1; i++) {
            nodeMap[i] = new Node();
        }
        Integer doubleIn = null;
        for (int i = 0; i < n; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            nodeMap[t].in++;
            if (!(nodeMap[t].in < 2)){
                doubleIn = t;
            }
            Edge edge = new Edge(s, t);
            edges.add(edge);
        }
        Edge result = null;
        if (doubleIn != null){
            List<Edge> doubleInEdges = new ArrayList<>();
            for (Edge edge : edges) {
                if (edge.t == doubleIn){
                    doubleInEdges.add(edge);
                }
                if (doubleInEdges.size() == 2){
                    break;
                }
            }
            Edge edge = doubleInEdges.get(1);
            if (isTreeWithExclude(edges, edge, nodeMap)){
                result = edge;
            } else {
                result = doubleInEdges.get(0);
            }
        } else {
            result = getRemoveEdge(edges, nodeMap);
        }
        System.out.println(result.s + " "+ result.t);
    }
    public static boolean isTreeWithExclude(List<Edge> edges, Edge exculdEdge, Node[] nodeMap){
        init(nodeMap.length);
        for (Edge edge : edges) {
            if (edge == exculdEdge){
                continue;
            }
            if (isSame(edge.s, edge.t)){
                return false;
            }
            join(edge.s, edge.t);
        }
        return true;
    }
    public static Edge getRemoveEdge(List<Edge> edges, Node[] nodeMap) {
        int length = nodeMap.length;
        init(length);
        for (Edge edge : edges) {
            if (isSame(edge.s, edge.t)) return edge;
            join(edge.s, edge.t);
        }
        return null;
    }
}
